﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChanExt;
using C.Entities;
using C.Unite;
using MySoft.Data;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
namespace ArtLand.OA.Project
{
    public partial class Login : System.Web.UI.Page
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Write();
            //ToolHelper.sendMail("yaxuan_sh@sina.com", "123", "123");
            //erpinfoservices.tbsService_ErpInfo te = new erpinfoservices.tbsService_ErpInfo();
            //erpinfoservices.MySoapHeader Header = new erpinfoservices.MySoapHeader();
            ////设置soap头变量
            //Header.UserName = "tbs";
            //Header.PassWord = "tbs123";
            //te.MySoapHeaderValue = Header;
            ////调用web 方法
            //Response.Write(te.UserLogin("ksaiguo","111111"));

            if (IsPostBack)
            {
                HttpCookie cookie = Request.Cookies[ConfigHelper.GetWebConfigPath("LoginCookGroup")];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-2);
                    Response.Cookies.Set(cookie);
                }
            }
        }
        #endregion

        #region 验证事件
        protected void ButClick(object sender, EventArgs e)
        {
            bool yz = true;
            string mes = "";
            if (Pwd.Text == "")
            {
                yz = false;
                mes = "密码不能为空！";
            }
            if (UserName.Text == "")
            {
                yz = false;
                mes = "用户名不能为空！";
            }

            if (yz)
            {
                Sys_Users _userInfo = TbsDbSession.Default.From<Sys_Users>().Where(Sys_Users._.UserName == UserName.Text && Sys_Users._.Pwd == StringHelper.md5(Pwd.Text, 16) && Sys_Users._.IsLock != true).ToFirst();
                if (_userInfo != null)
                {
                    HR_EMP em = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.EMPLID == _userInfo.UserName).ToFirst();
                    if (em != null)
                    {
                        #region 成功以后添加登录信息
                        System.Web.HttpCookie newcookie = new HttpCookie(ConfigHelper.GetWebConfigPath("LoginCookGroup"));
                        OA_Department _userGroup = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == em.DEPMETID).ToFirst();
                        if (_userGroup != null)
                        {
                            string pevtime = "";
                            if (_userInfo.LoginTime.HasValue)
                            {
                                Sys_Users user = new Sys_Users();
                                user.UserID = _userInfo.UserID;
                                user.Attach();
                                user.LoginPevTime = _userInfo.LoginTime;
                                TbsDbSession.Default.Update<Sys_Users>(user);
                                pevtime = _userInfo.LoginTime.Value.ToString();
                            }
                            else
                                pevtime = DateTime.Now.ToString();
                            if (_userInfo.Logins.HasValue)
                                UpdateLogs(_userInfo.UserID, _userInfo.Logins.Value);
                            else
                                UpdateLogs(_userInfo.UserID, 0);
                            LoginLog("登录成功", _userInfo.UserID, _userInfo.TrueName);

                            newcookie.Values["Uid"] = DesHelper.EncryptDES(_userInfo.UserID.ToString(), DesKeys.myKey);
                            newcookie.Values["NewGroupID"] = DesHelper.EncryptDES(_userGroup.DepID.ToString(), DesKeys.mygroupKey);
                            newcookie.Values["GroupName"] = _userGroup.DepName;
                            newcookie.Values["TrueName"] = HttpUtility.UrlEncode(em.EMNAME);
                            newcookie.Values["EmpID"] = _userInfo.UserName;
                            newcookie.Values["HREmpID"] = em.ID.ToString();
                            newcookie.Values["PevTime"] = pevtime;
                            newcookie.Expires = DateTime.Now.AddDays(1);
                            Response.AppendCookie(newcookie);
                            Response.Redirect("Index.aspx");
                        }
                        else
                        {
                            Alert.Show("信息未完整,请联系管理员！");
                        }
                        #endregion
                    }
                }
                else
                {
                    yz = false;
                    mes = "输入的用户名和密码不正确！";
                }
            }
            if (yz == false)
            {
                Alert.Show(mes);
            }
        }
        #endregion

        #region 重载页面
        protected void PageReLoad(object sender, EventArgs e)
        {
            PageContext.Refresh();
        }
        #endregion


        #region 添加登录日志
        public void LoginLog(string loginmsg, int uid, string truename)
        {
            Sys_UserLoginLog tll = new Sys_UserLoginLog();
            tll.DeAttach();
            tll.LoginDetail = loginmsg;
            tll.LoginIP = ToolHelper.getIPAddress();
            tll.LoginTime = DateTime.Now;
            tll.UserID = uid;
            tll.RealName = truename;
            TbsDbSession.Default.Insert<Sys_UserLoginLog>(tll);



        }
        #endregion

        #region 更新登录次数以及最后登录时间和IP
        public void UpdateLogs(int uid, int loginscount)
        {
            Sys_Users tll = new Sys_Users();
            tll.UserID = uid;
            tll.Attach();
            tll.LoginIP = ToolHelper.getIPAddress(); //HttpContext.Current.Request.ServerVariables["Remote_HOST"];
            tll.LoginTime = DateTime.Now;
            tll.Logins = loginscount + 1;
            TbsDbSession.Default.Update<Sys_Users>(tll);
        }
        #endregion
    }
}